package org.example.leetCode;

import com.sun.org.apache.regexp.internal.RE;

/**
 * @Author Qinkai
 * @Description TODO $
 * @Date $ 2021-09-18$
 * @Param $
 * @return $
 **/
public class IntToRoman {
    public String intToRoman(int num) {
        StringBuilder stringBuilder = new StringBuilder();
        if (num / 1000 > 0) {
            add(stringBuilder, num / 1000, "M");
            num %= 1000;
        }
        if (num / 100 == 9) {
            stringBuilder.append("CM");
            num %= 900;
        } else if (num / 100 == 4) {
            stringBuilder.append("CD");
            num %= 400;
        }
        if (num / 500 > 0) {
            add(stringBuilder, num / 500, "D");
            num %= 500;
        }
        if (num / 100 > 0) {
            add(stringBuilder, num / 100, "C");
            num %= 100;
        }
        if (num / 10 == 9) {
            stringBuilder.append("XC");
            num %= 90;
        } else if (num / 10 == 4) {
            stringBuilder.append("XL");
            num %= 40;
        }
        if (num / 50 > 0) {
            add(stringBuilder, num / 50, "L");
            num %= 50;
        }
        if (num / 10 > 0) {
            add(stringBuilder, num / 10, "X");
            num %= 10;
        }
        if (num == 9) {
            stringBuilder.append("IX");
            num %= 9;
        } else if (num == 4) {
            stringBuilder.append("IV");
            num %= 4;
        }
        if (num / 5 > 0) {
            add(stringBuilder, num / 5, "V");
            num %= 5;
        }
        add(stringBuilder, num, "I");

        return stringBuilder.toString();
    }

    private void add(StringBuilder stringBuilder, int count, String temp) {
        while (count > 0) {
            stringBuilder.append(temp);
            count--;
        }
    }
}
